<?php

/**
 * FCJ Members database library
 *
 * @author Felipe
 */
namespace FCJ\Database;



require_once ('php_Util_DB/php_Util_DB_PDO_lib.php');


/**
 * Namespace for PHP Util Database library
 *
*/
use php_Util\DB as ns_php_Util_DB;


use php_Util as ns_php_Util;

/**
 * Namespace for Database library
 *
 */
use FCJ\Database as ns_FCJ_Database;



/**
 * FCJ Members constants
 *
 */
class FCJ_DB_Members_CONSTANTS {
	
	const MEMBER_TYPE__STANDAR = 200;
}


/**
 * FCJ Members table
 *
 */
class FCJ_DB_Members_TABLE {
	public $tableName = 'fcj_members';
	public $field_id = 'id';
	public $field_member_type = 'member_type';
	public $field_group_ID = 'group_ID';
	public $field_name = 'name';
	public $field_surname = 'surname';
	public $field_member_Doc_ID = 'member_Doc_ID';
}



/**
 * FCJ Member data
 *
 */
class FCJ_DB_Member_DATA {
	public $id = null;
	public $member_type = null;
	public $group_ID = null;
	public $name = null;
	public $surname = null;
	public $member_Doc_ID = null;
}


/**
 * FCJ Members editor.
 *
 * Provides CRUD facilities
 *
 *
 */
class FCJ_DB_Members_EDITOR extends ns_php_Util_DB\php_Util_DB_EDITOR_PDO {

	/**
	 * @var FCJ_DB_Members_TABLE
	 */
	protected $table_Description = null;


	public function get_Description_Table() {
		return $this->table_Description;
	}


	public function __construct($table_Description = null) {

		if ($table_Description != null) {
			$this->table_Description = $table_Description;
		} else {
			$this->table_Description = ns_FCJ_Database\FCJ_DB_Members_FACTORY::get_NEW__FCJ_DB_Members_TABLE();
		}
	}


	/**
	 * Return array of insert fields for SQL insert
	 * @param FCJ_DB_Member_DATA $object_DATA
	 */
	public function get_Create_Fields($object_DATA) {

		$insertFields = array();

		$object_DATA->id !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_id => $object_DATA->id)) : null;
		$object_DATA->member_type !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_member_type => $object_DATA->member_type)) : null;
		$object_DATA->group_ID !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_ID => $object_DATA->group_ID)) : null;
		$object_DATA->name !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_name => $object_DATA->name)) : null;
		$object_DATA->surname !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_surname => $object_DATA->surname)) : null;
		$object_DATA->member_Doc_ID !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_member_Doc_ID => $object_DATA->member_Doc_ID)) : null;
		
		return $insertFields;
	}


	/**
	 * Return array of update fields for SQL insert
	 * @param FCJ_DB_Member_DATA $object_DATA
	 */
	public function get_Update_Fields($object_DATA) {

		$object_DATA_Update = clone $object_DATA;

		$object_DATA_Update->id = null;	// ID is a key. No update for this field.
		$updateFields = $this->get_Create_Fields($object_DATA_Update);

		return $updateFields;
	}


	/**
	 * Read DATA from row array
	 * @param FCJ_DB_Member_DATA $object_DATA
	 * @param array $row_Array
	 */
	public function get_DATA_From_RowArray($object_DATA, $row_Array) {

		$object_DATA->id = $row_Array[$this->table_Description->field_id];
		$object_DATA->member_type = $row_Array[$this->table_Description->field_member_type];
		$object_DATA->group_ID = $row_Array[$this->table_Description->field_group_ID];
		$object_DATA->name = $row_Array[$this->table_Description->field_name];
		$object_DATA->surname = $row_Array[$this->table_Description->field_surname];
		$object_DATA->member_Doc_ID = $row_Array[$this->table_Description->field_member_Doc_ID];
		
	}


	/**
	 * CRUD method for CREATE object
	 * @param FCJ_DB_Member_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Create($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---

		if (!is_a($object_DATA, "FCJ\Database\FCJ_DB_Member_DATA")) {

			throw new \Exception("FCJ_DB_Member_DATA is required.");
		}

		// -------------------------------------------------------------------------------------------------|/\|---


		// Create object -------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->create_Object($this->table_Description->tableName, $this->get_Create_Fields($object_DATA));

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot create object.");
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---


		// Insert last insert ID in object ------------------------------------------|-|---
		$object_DATA->id = $this->db_Connection->_connection_Link->lastInsertId();


		return $affected_Rows;
	}


	/**
	 * CRUD method for READ object
	 * @param FCJ_DB_Member_DATA $object_DATA
	 * @throws \Exception
	 *
	 */
	public function CRUD__Read($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_DATA')) {
			throw new \Exception("FCJ_DB_Member_DATA is required.");
		}

		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---



		// Read object ---------------------------------------------------------------------------------------------------|\/|---
		try {

			$result_DATA = $this->read_Object($this->table_Description->tableName, $conditions_Array);
			$this->get_DATA_From_RowArray($object_DATA, $result_DATA[0]);

		} catch (Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---

	}



	/**
	 * CRUD method for UPDATE object
	 * @param FCJ_DB_Member_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Update($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_DATA')) {
			throw new \Exception("FCJ_DB_Member_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---


		// Update object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->update_Object($this->table_Description->tableName, $this->get_Update_Fields($object_DATA), $conditions_Array);

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot update object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---


		return $affected_Rows;
	}




	/**
	 * CRUD method for DELETE object
	 * @param FCJ_DB_Member_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Delete($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_DATA')) {
			throw new \Exception("FCJ_DB_Member_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---


		// Delete object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->delete_Object($this->table_Description->tableName, $conditions_Array);

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot delete object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---


		return $affected_Rows;
	}

}


/**
 *
 * FCJ Members Database Factory
 *
 * Provides facilities for create Members related objects
 *
 * @author Felipe
 *
 */
class FCJ_DB_Members_FACTORY {


	public static function get_NEW__FCJ_DB_Members_TABLE() {
		return new ns_FCJ_Database\FCJ_DB_Members_TABLE();
	}


	public static function get_NEW__FCJ_DB_Member_DATA() {
		return new ns_FCJ_Database\FCJ_DB_Member_DATA();
	}


	public static function get_NEW__FCJ_DB_Members_EDITOR() {
		return new ns_FCJ_Database\FCJ_DB_Members_EDITOR();
	}


	/**
	 * Members search facility by conditions
	 * @param array|string $conditions
	 * @param php_Util_DB_CONNECTION $connection
	 * @return array
	 *
	 * @throws \Exception
	 */
	public static function get__Members_by_Conditions($conditions, $connection) {

		$fcj_DB_Members_EDITOR = self::get_NEW__FCJ_DB_Members_EDITOR();
		$fcj_DB_Members_TABLE = $fcj_DB_Members_EDITOR->get_Description_Table();


		// Set connection -------------------------------------------------------|\/|---
		try {
			$fcj_DB_Members_EDITOR->connect_To_DB($connection);
		} catch (\Exception $e) {
			throw new \Exception("Error in connection. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------|/\|---


		// Get result array -----------------------------------------------------------------|\/|---
		$result_Array = array();

		try {
			$result_Array = $fcj_DB_Members_EDITOR->read_Object($fcj_DB_Members_TABLE->tableName, $conditions);
		} catch (\Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------------------|/\|---


		// Get members data from result array ------------------------------------------------------------------------------|\/|---
		$array_Members_DATA = array();

		for ($index_Result = 0; $index_Result < sizeof($result_Array); $index_Result++) {
			$fcj_DB_Member_DATA = ns_FCJ_Database\FCJ_DB_Members_FACTORY::get_NEW__FCJ_DB_Member_DATA();
			$fcj_DB_Members_EDITOR->get_DATA_From_RowArray($fcj_DB_Member_DATA, $array_Members_DATA[$index_Result]);
			array_push($array_Members_DATA, $fcj_DB_Member_DATA);
		}
		// ----------------------------------------------------------------------------------------------------------------|/\|---


		return $array_Members_DATA;

	}

}






